//
//  SharingController.swift
//  BodyBuilding
//
//  Created by franze on 2017/9/30.
//  Copyright © 2017年 franze. All rights reserved.
//

import UIKit

class SharingController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    var collectionView:UICollectionView!
    var list:NSArray?
    lazy var refresh = UIRefreshControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewConfig()
        
        DispatchQueue.global(qos: .default).async {
            self.getList()
        }
        
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)

        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
        navBar.initialize(text: "晒图")
        navBar.backBtn.isHidden = true
        view.addSubview(navBar)
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: WIDTH/2, height: WIDTH/2)
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        
        collectionView = UICollectionView(frame: CGRect(x:0,y:64,width:WIDTH,height:HEIGHT-113-44), collectionViewLayout: flowLayout)
        collectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        view.addSubview(collectionView)
        
        refresh.addTarget(self, action: #selector(refreshData), for: .valueChanged)
        refresh.attributedTitle = NSAttributedString(string: "正在刷新")
        collectionView.addSubview(refresh)

        let addBtn = UIButton(frame: CGRect(x: 0, y: HEIGHT-93, width: 44, height: 44))
        addBtn.setImage(UIImage(named:"add")?.scaleImageToSize(CGSize(width: 38, height: 38)), for: .normal)
        addBtn.center.x = WIDTH/2
        view.addSubview(addBtn)
        
        addBtn.addTarget(self, action: #selector(add), for: .touchUpInside)
    }
    
    func getList(){
        let parameter:NSDictionary = ["SessionID":LoginModel.sessionID,"mod":"shaitu","related":"1"]
        Networking.shared.getSharingList(parameter: parameter) { (json) in
            self.list = json
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        }
    }
    
    @objc func refreshData(){
        getList()
        DispatchQueue.main.asyncAfter(deadline: .now()+1) {
            self.refresh.endRefreshing()
        }
    }
    
    @objc func add(){
        hidesBottomBarWhenPushed = true
        _ = navigationController?.pushViewController(SharingAddController(), animated: true)
        hidesBottomBarWhenPushed = false
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return list?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell
        let dict = list![indexPath.row] as! NSDictionary
        
        cell.label.text = dict.value(forKey: "title") as? String
        cell.imageView?.sd_setImage(with: URL(string: BB_IMAGEURLPRE+(dict.value(forKey: "thumb0") as! String)))
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let dict = list![indexPath.row] as! NSDictionary
        let commentArray = dict.value(forKey: "comment") as! NSArray
        let controller = SharingDetailController()
        controller.infoArray = commentArray.mutableCopy() as! NSMutableArray
        controller.userID = dict.value(forKey: "userid") as! String
        controller.shaituID = dict.value(forKey: "id") as! String
        
        hidesBottomBarWhenPushed = true
        _ = navigationController?.pushViewController(controller, animated: true)
        hidesBottomBarWhenPushed = false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
